package com.yj.suncity.activity.shouye;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.SigninActivity.OneKeyShareCallback;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.onekeyshare.ShowShare;
import com.yj.suncity.user.LoginActivity;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.Utils;
import com.yj.suncity.utis.DialogUtils.ReshActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class OfferDetailActivity extends Activity {
	private Context context;
	private boolean isLogin;
	private String userId;
	private SharedPreferencesUtil preferencesUtil;
	private ImageLoader imageLoader;
	private String offerId;
	private String endTime;
	private String offerContent;
	private String offerGiveNum;
	private String offerNum;
	private String entName;
	private String entLogo;
	private String entTel;

	private TextView mTxt_shop_name, mTxt_end_time, mTxt_give_num, mTxt_num,
			mTxt_offer_content;
	private ImageView mImg_logo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_offer_detail);
		context = this;
		preferencesUtil = SharedPreferencesUtil.getInstance();
		imageLoader=ImageLoader.getInstance();
		findView();
	}

	private void findView() {
		offerId = getIntent().getStringExtra("offerid");
		endTime = getIntent().getStringExtra("endtime");
		offerContent = getIntent().getStringExtra("offercontent");
		offerGiveNum = getIntent().getStringExtra("offergivenum");
		offerNum = getIntent().getStringExtra("offernum");
		entName = getIntent().getStringExtra("entname");
		entLogo = getIntent().getStringExtra("entlogo");
		entTel = getIntent().getStringExtra("enttel");
		Log.d("", "entLogo=="+entLogo);

		mImg_logo=(ImageView) findViewById(R.id.img_logo);
		mTxt_shop_name = (TextView) findViewById(R.id.txt_shop_name);
		mTxt_end_time = (TextView) findViewById(R.id.txt_end_time);
		mTxt_give_num = (TextView) findViewById(R.id.txt_give_num);
		mTxt_num = (TextView) findViewById(R.id.txt_num);
		mTxt_offer_content = (TextView) findViewById(R.id.txt_offer_content);

		imageLoader.displayImage(URLCon.HOST+entLogo, mImg_logo);
		mTxt_shop_name.setText(entName);
		mTxt_end_time.setText(Utils.DateFormatDate(endTime));
		mTxt_give_num.setText(offerGiveNum);
		mTxt_num.setText(offerNum);
		mTxt_offer_content.setText(offerContent);

	}

	@Override
	protected void onResume() {
		isLogin = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		if (isLogin) {
			userId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERID, "");
		}
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_jie_offer:
			if (isLogin) {
				getOffer();
			} else {
				startActivity(new Intent(context, LoginActivity.class));
			}
			break;
		case R.id.btn_share_offer:
			if (isLogin) {
				ShowShare share = new ShowShare(context, null,null);
				share.showShare(entName, offerContent, URLCon.HOST
						+ "/web/index.html", URLCon.HOST+entLogo);
			} else {
				startActivity(new Intent(context, LoginActivity.class));
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 接悬赏
	 */
	private void getOffer() {
		List<NameValuePair> pairs=new ArrayList<NameValuePair>();
		String path = URLCon.悬赏 ;
		pairs.add(new BasicNameValuePair("action", "acceptoffer"));
		pairs.add(new BasicNameValuePair("txtofferid", offerId));
		pairs.add(new BasicNameValuePair("txtuserid", userId));
		pairs.add(new BasicNameValuePair("type", "1"));
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, pairs, null,
				false, null, true, "正在加载......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							final DialogUtils dialogUtils = DialogUtils.getInstance();
							dialogUtils.backDialog1(context, "承接悬赏成功，请等待商家确认，或者与商家沟通！", true,
									new ReshActivity() {
										@Override
										public void reshActivity() {
											dialogUtils.CallDialog(context, entTel);
											
										}
									});
						} else {
							DialogUtils dialogUtils = DialogUtils.getInstance();
							dialogUtils.backDialog(context,
									jsonObject.optString("msg"), false, null);
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

}
